<ix-modal-header
  [requiredRoles]="requiredRoles"
  [title]="title"
  [loading]="isLoading()"
></ix-modal-header>

<mat-card>
  <mat-card-content>
    <form class="ix-form-container" [formGroup]="form" (submit)="onSubmit()">
      <ix-fieldset>
        <ix-select
          formControlName="name"
          [label]="'Name' | translate"
          [required]="true"
          [options]="isNew ? createIdmapNames$ : editIdmapNames$"
          [tooltip]="helptext.idmap.name.tooltip | translate"
        ></ix-select>

        @if (isCustomName) {
          <ix-input
            formControlName="custom_name"
            [label]="'Custom Name' | translate"
            [required]="true"
            [tooltip]="helptext.idmap.customNameTooltip | translate"
          ></ix-input>
        }

        @if (hasBackendField$ | async) {
          <ix-select
            formControlName="idmap_backend"
            [label]="'Idmap Backend' | translate"
            [required]="true"
            [options]="backends$"
            [tooltip]="helptext.idmap.idmapBackendTooltip | translate"
          ></ix-select>
        }

        <ix-input
          formControlName="dns_domain_name"
          [label]="'DNS Domain Name' | translate"
        ></ix-input>

        <ix-input
          formControlName="range_low"
          type="number"
          [label]="'Range Low' | translate"
          [required]="true"
          [tooltip]="helptext.idmap.rangeTooltip | translate"
        ></ix-input>

        <ix-input
          formControlName="range_high"
          type="number"
          [label]="'Range High' | translate"
          [required]="true"
          [tooltip]="helptext.idmap.rangeTooltip | translate"
        ></ix-input>

        @if (hasCertificateField$ | async) {
          <ix-with-manage-certificates-link>
            <ix-select
              formControlName="certificate"
              [label]="'Certificate' | translate"
              [options]="certificates$"
              [tooltip]="helptext.idmap.certificateIdTooltip | translate"
            ></ix-select>
          </ix-with-manage-certificates-link>
        }
      </ix-fieldset>

      <ix-fieldset [title]="'Options' | translate">
        @if (isOptionVisible('schema_mode')) {
          <ix-select
            formControlName="schema_mode"
            [label]="'Schema Mode' | translate"
            [options]="schemaModes$"
            [required]="true"
            [tooltip]="helptext.idmap.schemaModeTooltip | translate"
          ></ix-select>
        }

        @if (isOptionVisible('unix_primary_group')) {
          <ix-checkbox
            formControlName="unix_primary_group"
            [label]="'Unix Primary Group' | translate"
            [required]="isOptionRequired('unix_primary_group')"
            [tooltip]="helptext.idmap.unixPrimaryGroupTooltip | translate"
          ></ix-checkbox>
        }

        @if (isOptionVisible('unix_nss_info')) {
          <ix-checkbox
            formControlName="unix_nss_info"
            [label]="'Unix NSS Info' | translate"
            [required]="isOptionRequired('unix_nss_info')"
            [tooltip]="helptext.idmap.unixNssTooltip | translate"
          ></ix-checkbox>
        }

        @if (isOptionVisible('rangesize')) {
          <ix-input
            formControlName="rangesize"
            type="number"
            [label]="'Range Size' | translate"
            [required]="isOptionRequired('rangesize')"
            [tooltip]="helptext.idmap.rangesizeTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('readonly')) {
          <ix-checkbox
            formControlName="readonly"
            [label]="'Read Only' | translate"
            [required]="isOptionRequired('readonly')"
            [tooltip]="helptext.idmap.readonlyTooltip | translate"
          ></ix-checkbox>
        }

        @if (isOptionVisible('ignore_builtin')) {
          <ix-checkbox
            formControlName="ignore_builtin"
            [label]="'Ignore Builtin' | translate"
            [required]="isOptionRequired('ignore_builtin')"
            [tooltip]="helptext.idmap.ignoreBuiltinTooltip | translate"
          ></ix-checkbox>
        }

        @if (isOptionVisible('ldap_base_dn')) {
          <ix-input
            formControlName="ldap_base_dn"
            [label]="'Base DN' | translate"
            [required]="isOptionRequired('ldap_base_dn')"
            [tooltip]="helptext.idmap.ldapBasednTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ldap_user_dn')) {
          <ix-input
            formControlName="ldap_user_dn"
            [label]="'LDAP User DN' | translate"
            [required]="isOptionRequired('ldap_user_dn')"
            [tooltip]="helptext.idmap.ldapUserdnTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ldap_user_dn_password')) {
          <ix-input
            formControlName="ldap_user_dn_password"
            type="password"
            [label]="'LDAP User DN Password' | translate"
            [required]="isOptionRequired('ldap_user_dn_password')"
            [tooltip]="helptext.idmap.ldapUserDnPasswordTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ldap_url')) {
          <ix-input
            formControlName="ldap_url"
            [label]="'URL' | translate"
            [required]="isOptionRequired('ldap_url')"
            [tooltip]="helptext.idmap.ldapUrlTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ssl')) {
          <ix-select
            formControlName="ssl"
            [label]="'Encryption Mode' | translate"
            [options]="sslModes$"
            [required]="true"
          ></ix-select>
        }

        @if (isOptionVisible('linked_service')) {
          <ix-select
            formControlName="linked_service"
            [label]="'Linked Service' | translate"
            [options]="linkedServices$"
            [required]="true"
            [tooltip]="helptext.idmap.linkedServiceTooltip | translate"
          ></ix-select>
        }

        @if (isOptionVisible('ldap_server')) {
          <ix-input
            formControlName="ldap_server"
            [label]="'LDAP Server' | translate"
            [required]="isOptionRequired('ldap_server')"
            [tooltip]="helptext.idmap.ldapServerTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ldap_realm')) {
          <ix-input
            formControlName="ldap_realm"
            [label]="'LDAP Realm' | translate"
            [required]="isOptionRequired('ldap_realm')"
            [tooltip]="helptext.idmap.ldapRealmTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('bind_path_user')) {
          <ix-input
            formControlName="bind_path_user"
            [label]="'User Bind Path' | translate"
            [required]="isOptionRequired('bind_path_user')"
            [tooltip]="helptext.idmap.bindPathUserTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('bind_path_group')) {
          <ix-input
            formControlName="bind_path_group"
            [label]="'Group Bind Path' | translate"
            [required]="isOptionRequired('bind_path_group')"
            [tooltip]="helptext.idmap.bindPathGroupTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('user_cn')) {
          <ix-input
            formControlName="user_cn"
            [label]="'User CN' | translate"
            [required]="isOptionRequired('user_cn')"
            [tooltip]="helptext.idmap.userCnTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('cn_realm')) {
          <ix-input
            formControlName="cn_realm"
            [label]="'CN Realm' | translate"
            [required]="isOptionRequired('cn_realm')"
            [tooltip]="helptext.idmap.cnRealmTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('ldap_domain')) {
          <ix-input
            formControlName="ldap_domain"
            [label]="'LDAP Domain' | translate"
            [required]="isOptionRequired('ldap_domain')"
            [tooltip]="helptext.idmap.ldapDomainTooltip | translate"
          ></ix-input>
        }

        @if (isOptionVisible('sssd_compat')) {
          <ix-checkbox
            formControlName="sssd_compat"
            [label]="'SSSD Compat' | translate"
            [required]="isOptionRequired('sssd_compat')"
            [tooltip]="helptext.idmap.sssdCompatTooltip | translate"
          ></ix-checkbox>
        }
      </ix-fieldset>
      <ix-form-actions>
        <button
          *ixRequiresRoles="requiredRoles"
          type="submit"
          mat-button
          color="primary"
          ixTest="save"
          [disabled]="form.invalid || isLoading()"
        >
          {{ 'Save' | translate }}
        </button>
      </ix-form-actions>
    </form>
  </mat-card-content>
</mat-card>
